package com.jsyh.maidu.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.jsyh.maidu.R;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.model.GoodsAttr;
import com.jsyh.maidu.model.GoodsAttrModel;
import com.jsyh.maidu.model.GoodsInfoModel2;
import com.jsyh.maidu.presenter.ParamPresenter;
import com.jsyh.maidu.views.GoodsAttrView;
import com.jsyh.shopping.uilibrary.views.AddAndSubView;
import com.squareup.picasso.Picasso;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/7/20.
 */

public class ParamPopupWindow implements GoodsAttrView, View.OnClickListener,
        AddAndSubView.OnNumChangeListener {

    private PopupWindow pop;
    Context context;
    View container;
    ParamPresenter presenter;
    GoodsAttr attr;

    private ImageView paramImg;
    private TextView paramPrice;
    private TextView paramNum;
    private TextView paramSelect;
    private ImageView closeParam;
    private Button submit;
    private AddAndSubView addAndSubView;
    private LinearLayout sumLayout;

    private ViewGroup layout_Attribute;
    List<GoodsInfoModel2.Attribute> datas;
    OnSubmitListener listener;
    String goods_id;
    String attr_value_id;
    String attr_id;
    String num;
    String[] arr;
    String[] attribute;

    public ParamPopupWindow(Context context, List<GoodsInfoModel2.Attribute> datas,
                            String goods_id, String attr_value_id, String num, OnSubmitListener listener, String attr_id) {
        this.context = context;
        this.datas = datas;
        this.listener = listener;
        this.goods_id = goods_id;
        this.attr_value_id = attr_value_id;
        this.num = num;
        this.attr_id = attr_id;
        container = LayoutInflater.from(context).inflate(R.layout.param_pop, null);
        pop = new PopupWindow(container, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        paramImg = (ImageView) container.findViewById(R.id.param_goods_image);
        paramPrice = (TextView) container.findViewById(R.id.param_goods_price);
        paramNum = (TextView) container.findViewById(R.id.param_goods_sum);
        paramSelect = (TextView) container.findViewById(R.id.param_goods_param);
        closeParam = (ImageView) container.findViewById(R.id.close_pop);
        layout_Attribute = (ViewGroup) container.findViewById(R.id.layout_Attribute);
        submit = (Button) container.findViewById(R.id.pop_submit);
        addAndSubView = (AddAndSubView) container.findViewById(R.id.goodsNum);
        sumLayout = (LinearLayout) container.findViewById(R.id.sum_layout);
        if (!TextUtils.isEmpty(num)) {
            addAndSubView.setNum(Integer.valueOf(num));
        } else {
            addAndSubView.setNum(1);
        }
        addAndSubView.setOnNumChangeListener(this);
        submit.setOnClickListener(this);
        presenter = new ParamPresenter(this);
        closeParam.setOnClickListener(this);
        if (TextUtils.isEmpty(attr_value_id) && datas.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (GoodsInfoModel2.Attribute attr : datas) {
                sb.append(attr.getAttr_value().get(0).getAttr_value_id()).append(",");
            }
            attr_value_id = sb.substring(0, sb.length() - 1).toString();
        }
        presenter.getGoodsAttr(context, goods_id, attr_value_id, num);
        if (datas.size() > 0) {
            initParam();
        }
    }

    public void setSumGone(int visiable) {
        sumLayout.setVisibility(visiable);
    }

    private void initParam() {
        final List<GoodsInfoModel2.Attribute> attributes = datas;
        if (!TextUtils.isEmpty(attr_value_id)) {
            if (attr_value_id.contains(",")) {
                arr = attr_value_id.split(",");
            } else {
                arr = new String[1];
                arr[0] = attr_value_id;
            }
        } else {
            arr = new String[attributes.size()];
        }
        if (!TextUtils.isEmpty(attr_id)) {
            if (attr_id.contains(",")) {
                attribute = attr_id.split(",");
            } else {
                attribute = new String[1];
                attribute[0] = attr_id;
            }
        } else {
            attribute = new String[attributes.size()];
        }
        for (int i = 0; i < attributes.size(); i++) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.fragment_single_choose, null);
            rootView.setId(10000 + i);
            layout_Attribute.addView(rootView);
            TextView tv_attribute = (TextView) rootView.findViewById(R.id.tv_attribute);
            tv_attribute.setText(attributes.get(i).getAttr_name());
            final TagFlowLayout tagFlowLayout_attribute_value = (TagFlowLayout) rootView.findViewById(R.id.tagFlowLayout_attribute_value);
            final int finalI = i;
            tagFlowLayout_attribute_value.setAdapter(new TagAdapter<GoodsInfoModel2.Attribute.Attr_Value>(attributes.get(i).getAttr_value()) {
                @Override
                public View getView(FlowLayout parent, int position, GoodsInfoModel2.Attribute.Attr_Value t) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.select_attribute_item, tagFlowLayout_attribute_value, false);
                    tv.setText(t.attr_value_name);
                    return tv;
                }
            });
            tagFlowLayout_attribute_value.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {

                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    arr[finalI] = attributes.get(finalI).getAttr_value().get(position).getAttr_value_id();
                    StringBuilder sb = new StringBuilder();
                    for (String str : arr) {
                        sb.append(str).append(",");
                    }
                    attribute[finalI] = attributes.get(finalI).getAttr_id() + "-" + attributes.get(finalI).getAttr_value().get(position).getAttr_value_id();
                    StringBuilder sb2 = new StringBuilder();
                    for (String str : attribute) {
                        sb2.append(str).append(",");
                    }
                    attr_value_id = sb.substring(0, sb.length() - 1).toString();
                    attr_id = sb2.substring(0, sb2.length() - 1).toString();
                    presenter.getGoodsAttr(context, goods_id, attr_value_id, num);
                    return true;
                }
            });

        }
    }


    public void show(View targetView) {
        show(targetView, Gravity.BOTTOM);
    }

    public void show(View targetView, int gravity) {
        pop.setAnimationStyle(-1);
        pop.showAtLocation(targetView, gravity, 0, 0);
    }

    @Override
    public void getGoodsAtrr(GoodsAttrModel model) {
        if (model.getCode().equals("1")) {
            attr = model.getData();
            Picasso.with(context).load(ConfigValue.IMG_IP + attr.getImage()).into(paramImg);
            paramPrice.setText("价格：" + attr.getTotal_price());
            paramNum.setText("库存：" + attr.getProduct_number() + "件");
            paramSelect.setText(attr.getChecked());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pop_submit:
                if (attr != null) {
                    attr.setAttr_value_id(attr_value_id);
                    attr.setAttr_id(attr_id);
                    attr.setNum(num);
                    if (Integer.valueOf(num) <= 0) {
                        Toast.makeText(context, "请正确选择商品数量", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    listener.submit(attr);
                    pop.dismiss();
                }
                break;
            case R.id.close_pop:
                if (pop.isShowing()) {
                    pop.dismiss();
                }
                break;
        }
    }

    @Override
    public void onNumChange(View view, int num) {
        this.num = num + "";
        if (num > 0) {
            presenter.getGoodsAttr(context, goods_id, attr_value_id, this.num);
        } else {
            Toast.makeText(context, "请正确选择商品数量", Toast.LENGTH_SHORT).show();
        }
    }

    public interface OnSubmitListener {
        void submit(GoodsAttr attr);
    }
}
